<?php
/**
 * File: MigrateController.class.php
 * Author: camy <375766145@qq.com>
 * Time: 2018/3/20 11:56
 */

namespace Command\Controller;
/**
 * 数据库配置
 * @package Command\Controller
 */
class MigrateController extends ConsoleController
{
    public function Index()
    {
        echo '初始化数据库：' . PHP_EOL;
        $filePath = dirname(__DIR__) . '/SqlFile/';
        try {
            M()->startTrans();
            echo "正在导入数据表..." . PHP_EOL;
            $dbSql = file_get_contents($filePath . 'db.sql');
            $res = M()->execute($dbSql);
            if ($res === false) {
                throw new \Exception('创建数据表失败!');
            }
            echo "正在导入地址数据..." . PHP_EOL;
            $regionSql = file_get_contents($filePath . '../SqlFile/region.sql');
            $res = M()->execute($regionSql);
            if ($res === false) {
                throw new \Exception('导入地址数据失败!');
            }
            M()->commit();
            echo '初始化数据库成功！' . PHP_EOL;
            return self::ExitOK;
        } catch (\Exception $e) {
            M()->rollback();
            echo '初始化数据库失败：' . $e->getMessage() . PHP_EOL;
            return self::ExitFAIL;
        }
    }


}